<?php

	$TEMP->set_file(array('handle' => DIR_IFRAME_VIEW . 'registrationNewComers.html'));
	
	$departmentList = $DSC->getListDepartment();
	
	if ( isset($_POST['subButton']) ) {
		
		$if_error		= 0;
		$if_display		= 0;
		$txtcourse		= _POST('txtcourse');
		$txtaccountno	= _POST('txtaccountno');
		$txtdepartment	= _POST('txtdepartment');
		$txtfname		= _POST('txtfname');
		$txtmname		= _POST('txtmname'); 
		$txtlname		= _POST('txtlname');
		$txtemail		= _POST('txtemail');
		$txtcontact		= _POST('txtcontact');
		
		$txtusername	= _POST('txtusername');
		$txtpassword	= _POST('txtpassword', false);
		
		if ( empty($txtcourse) || $txtcourse == 0 )	{ $if_error++; $_txtcourse 	= REQ_FIELD; }
		if ( empty($txtaccountno) )	{ $if_error++; $_txtaccountno 				= REQ_FIELD; }
		if ( empty($txtfname) )		{ $if_error++; $_txtfname 					= REQ_FIELD; }
		if ( empty($txtlname) )		{ $if_error++; $_txtlname 					= REQ_FIELD; }
		
		if ( empty($txtemail) )		{ $if_error++; $_txtemail 					= REQ_FIELD; }
		else {
			$emailReturn = $EMAIL->validateEmail($txtemail);
			if ($emailReturn === TRUE) {
				 $if_error++;
				$_txtemail 	= INVALID_EMAIL;
			}
		}
		
		if ( empty($txtusername) )	{ $if_error++; $_txtusername 	= REQ_FIELD; }
		if ( empty($txtpassword) )	{ $if_error++; $_txtpassword 	= REQ_FIELD; }
		
		if ( $if_error == 0 ) {
			$strArray = array('course_id'	=> $txtcourse,
							  'stud_no' 	=> upperCase($txtaccountno),
							  'first_name' 	=> firstUpperCase($txtfname),
							  'last_name' 	=> firstUpperCase($txtlname),
							  'middle_name' => firstUpperCase($txtmname),
							  'email' 		=> $txtemail,
							  'contact_no' 	=> $txtcontact,
							  'log_user' 	=> lowerCase($txtusername),
							  'log_pass' 	=> $txtpassword);
							  
			$STUD->insertStudentInformation($strArray, false);
			$saved_record = 1;
		}
	}
	else {
		$lastStudentID = $STUD->getLastStudentID();
		if ( empty($lastStudentID) )
			$txtaccountno 	= 'S'.date("y").'-00001';
		else {
			$nextID = $lastStudentID['stud_idPK'] + 1;
			$txtaccountno 	= 'S'.date("y").'-'.sprintf("%05d", $nextID);
		}
	}
	
	$selectDepartmentList = '<select name="txtdepartment" id="txtdepartment" class="select-auto-input" onchange="courseDept();">';
		foreach($departmentList as $departmentListVal) {
			$selectedDept = ( $txtdepartment == $departmentListVal['dept_idPK'] ) ? 'selected' : '';
			$selectDepartmentList .= '<option value="' . $departmentListVal['dept_idPK'] . '" ' . $selectedDept . '>' . $departmentListVal['dept_desc'] . '</option>';
		}
	$selectDepartmentList .= '</select>';
	
	
	$TEMP->set_var(array('txtaccountno' 	=> $txtaccountno,
						 'txtcourse'		=> $txtcourse,
						 'txtfname' 		=> $txtfname,
						 'txtmname' 		=> $txtmname,
						 'txtlname' 		=> $txtlname,
						 'txtemail'			=> $txtemail,
						 'txtcontact'		=> $txtcontact,
						 'txtusername'		=> $txtusername,
						 'w_txtcourse' 		=> $_txtcourse,
						 'w_txtaccountno' 	=> $_txtaccountno,
						 'w_txtfname' 		=> $_txtfname,
						 'w_txtlname' 		=> $_txtlname,
						 'w_txtemail' 		=> $_txtemail,
						 'w_txtusername'	=> $_txtusername,
						 'w_txtpassword'	=> $_txtpassword,
						 ));
	
	$TEMP->set_var(array('selected'.$txtaccess 	=> 'selected',
						 'saved_record'			=> $saved_record,
						 'department_list'		=> $selectDepartmentList,
						 'show_dept'			=> $txtdepartment,
						 'm'					=> date("m"),
						 'd'					=> date("d"),
						 'y'					=> date("y"),
						 'h'					=> date("h"),
						 'i'					=> date("i"),
						 ));
						 
?>